# calculate Spearman correlation matrix
for (i in 1:12) {
  print(paste0(">>> Iterate month: ", i))
  
  d <- read.csv(paste0("output/data_monthly/", i , ".csv"))
  d <- na.omit(d)
  
  catch <- d$catch
  dd <- d[,-which(names(d) %in% c("year", "catch"))]
  dnames <- names(dd)
  
  
  x <- c()
  y <- c()
  rho <- c()
  pvalues <- c()
  for (j in 1:length(dnames)) {
    val <- dd[,dnames[j]]
    
    ct <- cor.test(val, catch)
    r <- ct$estimate
    pval <- ct$p.value
    
    rho <- c(rho, r)
    pvalues <- c(pvalues, pval)
    xy <- str_split(dnames[j], "_")
    x <- c(x, xy[[1]][1])
    y <- c(y, xy[[1]][2])
  }
  
  df <- data.frame(xy = dnames, x = x, y = y, rho = rho, p = pvalues)
  write.csv(df, paste0("output/cor_monthly/", i, ".csv"))
}